using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using UnityEditor;
using UnityEngine;
using XEntity.InventoryItemSystem;

/// <summary>
/// 用来在蛊效果类脚本构造完之后， 创建对应的蛊资源和Item资源
/// </summary>
public class GurouceMaker
{
 
#if UNITY_EDITOR
    public static void Make(string name, Type findTarget, Type influence, Type startCondition, Type collider2D = null, string spriteName = null, string iconPath = null){
        UnityEngine.Debug.Log("Make Gurouce"); 
        string spritePath = null;
        if (spriteName!=null){
            //将对应name的png转化为Sprite并进行加载
            string spritePNGPath = "Assets/HotUpdate/Gurouce/"+name+"Gurouce/"+spriteName+".png";
            TextureImporter textureImporter = AssetImporter.GetAtPath(spritePNGPath) as TextureImporter;
            if (textureImporter != null)
            {
                textureImporter.textureType = TextureImporterType.Sprite;
                Texture2D texture = AssetDatabase.LoadAssetAtPath<Texture2D>(spritePNGPath);
                textureImporter.spritePixelsPerUnit = texture.height;
                textureImporter.SaveAndReimport();
            }
            spritePath = spritePNGPath;
        }

        GameObject prefabOfGurouce = CreateGuroucePerfab(name, findTarget, influence, startCondition, collider2D, spritePath);
        CreateItem(name, prefabOfGurouce, spritePath);
        
        AddAssetsToAddressables.AddAssets();
    }

    public static GameObject CreateGuroucePerfab(string name, Type findTarget, Type influence, Type startCondition, Type collider2D = null, string spritePath=null){
         
        UnityEngine.Debug.Log("Params Check");
        
        string assemblyPath = "Assets/HotUpdate/DLLs/HotUpdate.dll.bytes";
        Assembly hotUpdateAssembly = Assembly.LoadFrom(assemblyPath);
        Type baseFindTargetType = hotUpdateAssembly.GetType("BaseFindTarget");
        Type baseInfluenceType = hotUpdateAssembly.GetType("BaseInfluence");
        Type baseStartConditionType = hotUpdateAssembly.GetType("BaseStartCondition");
        if (!baseFindTargetType.IsAssignableFrom(findTarget)) throw new Exception("findTarget must be a subclass of BaseFindTarget");
        if (!baseInfluenceType.IsAssignableFrom(influence)) throw new Exception("influence must be a subclass of BaseInfluence");
        if (!baseStartConditionType.IsAssignableFrom(startCondition)) throw new Exception("startCondition must be a subclass of BaseStartCondition");
        if (collider2D==null) collider2D = typeof(BoxCollider2D); 
        if (!typeof(Collider2D).IsAssignableFrom(collider2D)) throw new Exception("collider2D must be a subclass of Collider2D");

        UnityEngine.Debug.Log("Create Gurouce Prefab");

        GameObject prefab = new GameObject(name+"Gurouce"); 
        UnityEngine.Debug.Log("Add Component");
        UnityEngine.Debug.Log("Add Component : "+findTarget);
        UnityEngine.Debug.Log("Add Component : "+startCondition);
        
        SpriteRenderer sr = prefab.AddComponent<SpriteRenderer>();
        Sprite t = null;
        if (spritePath == null)
            t = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/HotUpdate/Gurouce/BaseScripts/DefaultSprite.asset");
        else{
            t = AssetDatabase.LoadAssetAtPath<Sprite>(spritePath);
        }
        sr.sprite = t;
        sr.sortingOrder = 1;
        prefab.AddComponent(collider2D); 
        prefab.AddComponent(findTarget); 
        prefab.AddComponent(startCondition); 
        var rb = prefab.GetComponent<Rigidbody2D>();
        rb.gravityScale = 0; 
        Collider2D co2d = prefab.GetComponent<Collider2D>();
        co2d.isTrigger = true;

        UnityEngine.Debug.Log("Gurouce Prefab Save");

        string guroucePath = "Assets/HotUpdate/Gurouce/"+name+"Gurouce/"+name+"Gurouce.prefab";
        GameObject returnPreturn = PrefabUtility.SaveAsPrefabAsset(prefab, guroucePath);
        GameObject.DestroyImmediate(prefab);
        return returnPreturn;
    }

    
    public static void CreateItem(string name, GameObject prefabOfGurouce, string iconPath = null){
        UnityEngine.Debug.Log("Create Item");

        Item item = ScriptableObject.CreateInstance<Item>();
        item.type = ItemType.Gurouce;
        item.itemName = name+"Gurouce";
        item.GurouceName = name;
        item.itemPerSlot = 1;
        item.prefab = prefabOfGurouce;
        if (iconPath!=null){ 
            item.icon = AssetDatabase.LoadAssetAtPath<Sprite>(iconPath);
        }else{
            item.icon = AssetDatabase.LoadAssetAtPath<Sprite>("Assets/HotUpdate/Gurouce/BaseScripts/DefaultIcon.png");
        }
        UnityEngine.Debug.Log("Save Item"); 
        AssetDatabase.CreateAsset(item, "Assets/HotUpdate/Gurouce/"+name+"Gurouce/"+item.itemName+"Item.asset");
    }



# endif
}
